// pages/project_member/project_member.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectId: null,
    membersArr: [],
    modalShow: false,
    isManager: 0,  // 当前用户是否项目负责人
    score: 0,      // 积分
    description: '', // 积分工作描述
    userId: null,  // 被添加积分的用户 id
    userInfo: null,// 当前登录的用户信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.setData({
      projectId: options.projectId,
      isManager: options.isManager
    })

    wx.setNavigationBarTitle({
      title: options.projectName,
    })
    this.reqCurrUserInfo();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 根据 projectId 获取该项目对应所有的社员
    this.reqProjectAllMembers();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.reqProjectAllMembers();
    this.reqIsProjectManager();  // 获取当前用户是否项目负责人
    wx.stopPullDownRefresh();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '项目成员'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '项目成员'
    }
  },

  /**
   * 获取所有的项目社员
   */
  reqProjectAllMembers() {
    request.GET('/project/getProjectMembers', {projectId: this.data.projectId}).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        this.setData({
          membersArr: res.data.memberArr
        })
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error'
        })
      }
    })
  },

  /**
   * 判断当前用户数是否项目负责人
   */
  reqIsProjectManager() {
    const param = {
      projectId: this.data.projectId,
      userId: wx.getStorageSync('userId')
    }
    request.GET("/project/getProjectManagerFlag", param).then(res => {
      // console.log(res)
      const isManager = res.data=='y'? 1: 0;
      this.setData({
        isManager: isManager 
      })
    });
  },

  /**
   * 获取当前用户信息
   */
  reqCurrUserInfo() {
    request.GET('/user/userInfo', {userId: wx.getStorageSync('userId')}).then(res => {
      // console.log(res)
      let userInfo = null;
      if (res.data.resCode==200) {
        userInfo = res.data.userInfo;
      }
      this.setData({
        userInfo: userInfo
      })
    })
  },

  /**
   * 隐藏 modal
   */
  clickHideModal() {
    this.setData({
      modalShow: false,
      userId: null,
      score: 0,
      description: ''
    })
  },

  /**
   * 点击添加社员
   */
  gotoAddProjectMember() {
    wx.navigateTo({
      url: '/pages/project_member_add/project_member_add?projectId='+ this.data.projectId,
    })
  },

  /**
   * 点击查看社员详情
   */
  gotoMembershipDetail(e) {

    let canIView = false;
    let content = '等级限制, 无法查看该社员信息';
    const vip = e.currentTarget.dataset.vip;
    const userId = e.currentTarget.dataset.userid;
    
    if (this.data.userInfo) {
      if (this.data.userInfo['id']==userId) {
        canIView = true;
      } else {
        if (this.data.userInfo['vip']!=0) {
          if (this.data.userInfo['vip']==100) {
            canIView = true;  // 决策社员允许看所有人的
          } else {
            if (vip!=0) {  // 限制普通社员不能看普通用户
              canIView = true;
            } else {
              content = '社员只能查看其他社员信息，不能查看普通成员信息';
            }
          }
        } else {
          content = '成为社员，才能查看其他社员的信息';
        }
      }
    } else {
      content = '您尚未登录, 无法查看该社员信息';
    }

    if (!canIView) {
      wx.showModal({
        title: '温馨提示',
        content: content,
        showCancel: false
      })
      return;
    }

    wx.navigateTo({
      url: '/pages/membership_detail/membership_detail?userId='+ userId +'&projectId='+ this.data.projectId,
    })
  },

  /**
   * 点击跳转添加积分
   */
  clickAddScore(e) {
    // this.setData({
    //   modalShow: true,
    //   userId: e.currentTarget.dataset.userid
    // })
    wx.navigateTo({
      url: '/pages/project_score_add/project_score_add?projectId='+ this.data.projectId +'&userId='+ e.currentTarget.dataset.userid,
    })
  },

  /**
   * 点击删除成员
   */
  delFromProjectMember(e) {
    console.log(e.currentTarget.dataset.userid)
    const that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除该成员吗?',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          const param = {projectId: that.data.projectId, userId: e.currentTarget.dataset.userid}
          // request.GET('/project/delProjectMember', param).then(res=>{
          //   console.log(res)
          //   if (res.data.resCode==200) {
              
          //   } else {
              
          //   }
          // })
        }
      }
    })
  },

  /**
   * 积分输入的改变
   */
  bindScoreInput(e) {
    this.setData({
      score: e.detail.value
    })
  },

  /**
   * 积分工作描述
   */
  bindDescriptionInput(e) {
    this.setData({
      description: e.detail.value
    })
  },

  /**
   * 点击 modal 中的确定按钮
   */
  clickModalSave() {
    if (this.data.score<=0) {
      wx.showToast({
        title: '积分不能为空或小于 0',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    this.setData({
      modalShow: false
    })

    wx.showToast({
      title: '正在添加...',
      icon: 'loading',
      duration: 30000,
      mask: true
    })

    // 提交积分
    const param = {
      projectId: this.data.projectId,
      userId: this.data.userId,
      addUserId: wx.getStorageSync('userId'), 
      score: this.data.score,
      description: this.data.description.trim()
    };
    request.GET('/project/saveProjectScore', param).then(res => {
      // console.log(res);
      if (res.data.resCode==200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '添加异常',
          icon: 'error',
          duration: 1500,
          mask: true
        })
      }
    })
  },

})